﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace RacewayId
{
    public partial class Form1 : Form
    {
        private List<Raceway> raceways;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            raceways = Work.Init();
            splitContainer1.SplitterDistance = txtDwg.Height;
            splitContainer2.SplitterDistance = Width / 2;
        }

        private void txtDwg_TextChanged(object sender, EventArgs e)
        {
            string dwgString = txtDwg.Text.Trim().ToUpper();
            string idString = txtId.Text.Trim().ToUpper();
            Thread thread = new Thread(new ThreadStart(() =>
            {
                var result = raceways.AsEnumerable();
                if (!string.IsNullOrWhiteSpace(dwgString))
                {
                    result = result.Where(r => r.Draw.Contains(dwgString));
                }
                if (!string.IsNullOrWhiteSpace(idString))
                {
                    result = result.Where(r => r.RaceWayid.Contains(idString));
                }
                result = result.OrderBy(r => r.Id);
                ShowReslut(result);

            }));
            thread.Start();


        }
        private void ShowReslut(IEnumerable<Raceway> raceways)
        {

            Action action = () =>
            {

                listboxShow.Items.Clear();
                foreach (var raceway in raceways)
                {
                    listboxShow.Items.Add(raceway.RaceWayid);

                }
            };
            BeginInvoke(action);


        }

        private void listboxShow_SelectedIndexChanged(object sender, EventArgs e)
        {


            Clipboard.SetDataObject(listboxShow.SelectedItem.ToString());
        }



    }
}

